﻿
Namespace SQLDataAccess

    Public NotInheritable Class DataAccessConnection

#Region " Declarations & Enums "

        '.NET guarantees thread safety for shared initialization
        Private Shared _instance As New DataAccessConnection
        Private Shared _objConnectionStrings As New Dictionary(Of String, String)

#End Region

#Region " Constructors "

        Private Sub New()
        End Sub

#End Region

#Region " Methods "

        ''' <summary>
        ''' Factory that returns the shared instance of DataAccessConnection
        ''' </summary>
        Public Shared Function CreateInstance() As DataAccessConnection
            Return _instance
        End Function

        ''' <summary>
        ''' Using a Key, looks up a previously stored connection string.
        ''' </summary>
        ''' <param name="strConnectionStringKey">String ConnectionStringKey is used to look up a previously stored connection string using the LoadConnectionString method.</param>
        ''' <exception cref="ArgumentOutOfRangeException">Thrown when the Key was not found in the collection.</exception>
        ''' <returns>Database connection string</returns>
        Public Shared Function GetConnectionString(ByVal strConnectionStringKey As String) As String

            If _objConnectionStrings.ContainsKey(strConnectionStringKey) Then
                Return _objConnectionStrings.Item(strConnectionStringKey)

            Else
                Throw New ArgumentOutOfRangeException("ConnectionStringKey", strConnectionStringKey, "The ConnectionStringKey was not found in the collection of connection strings.")
            End If

        End Function

        ''' <summary>
        ''' Loads a connection string 
        ''' </summary>
        ''' <param name="strConnectionStringKey">String ConnectionStringKey</param>
        ''' <param name="strConnectionString">Connection string</param>
        Public Shared Sub LoadConnectionString(ByVal strConnectionStringKey As String, ByVal strConnectionString As String)
            _objConnectionStrings.Add(strConnectionStringKey, strConnectionString)
        End Sub

#End Region

    End Class

End Namespace
